package net.ibizsys.eclipse.studio.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;

/**
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class FileKit
{
	/**
	 * 获取文件的后缀名
	 * 
	 * @param filename
	 * @return
	 */
	public static String suffix(String filename)
	{
		if ((filename != null) && (filename.length() > 0))
		{
			int nPos = filename.lastIndexOf('.');
			if (nPos != -1 && nPos < (filename.length() - 1))
			{
				return filename.substring(nPos + 1);
			}
		}
		return filename;
	}

	/**
	 * 获取文件名<不包含后缀名>
	 * 
	 * @param filename
	 * @return
	 */
	public static String prefix(String filename)
	{
		if (filename != null)
		{
			int dot = filename.lastIndexOf('.');
			if ((dot > -1) && (dot < (filename.length())))
			{
				return filename.substring(0, dot);
			}
		}
		return filename;
	}

	/**
	 * 删除
	 * 
	 * @param path
	 */
	public static void delete(File path)
	{
		if (!path.exists())
		{
			return;
		}
		if (path.isFile())
		{
			path.delete();
			return;
		}
		File[] files = path.listFiles();
		for (File file : files)
		{
			delete(file);
		}
		path.delete();
	}

	/**
	 * 创建目录
	 * 
	 * @param parent 父文件夹
	 * @param name 文件夹
	 * @return
	 */
	public static boolean mkdirs(File parent, String name)
	{
		return new File(parent, name).mkdirs();
	}

	/**
	 * 创建文件夹<文件夹已存在返回True>
	 * 
	 * @param dir 文件夹
	 * @return
	 */
	public static boolean mkdirs(File dir)
	{
		if (dir.exists()) return true;

		return dir.mkdirs();
	}

	/**
	 * 读取文件全部内容
	 * 
	 * @param source
	 * @return
	 */
	public static String readFile(File file)
	{
		InputStream in = null;
		BufferedReader br = null;
		StringBuilder builder = new StringBuilder();
		try
		{
			in = new FileInputStream(file);
			br = new BufferedReader(new InputStreamReader(in, "UTF-8"));

			String buffer = null;
			while ((buffer = br.readLine()) != null)
			{
				builder.append(buffer);
			}
		} catch (Exception e)
		{

		} finally
		{
			try
			{
				if (in != null) in.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			try
			{
				if (br != null) br.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		return builder.toString();
	}

	/**
	 * 读取文件全部内容
	 * 
	 * @param source
	 * @return
	 */
	public static String readFile(URL source)
	{
		InputStream in = null;
		BufferedReader br = null;
		StringBuilder builder = new StringBuilder();
		try
		{
			in = source.openConnection().getInputStream();
			br = new BufferedReader(new InputStreamReader(in));

			String buffer = null;
			while ((buffer = br.readLine()) != null)
			{
				builder.append(buffer);
			}
		} catch (Exception e)
		{

		} finally
		{
			try
			{
				if (in != null) in.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			try
			{
				if (br != null) br.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		return builder.toString();
	}

	/**
	 * 复制文件
	 * 
	 * @param source
	 * @param target
	 * @return
	 */
	public static boolean copyFile(URL source, File target)
	{
		int byteread = 0;
		InputStream in = null;
		OutputStream out = null;

		try
		{
			in = source.openConnection().getInputStream();
			out = new FileOutputStream(target);
			byte[] buffer = new byte[1024];
			while ((byteread = in.read(buffer)) != -1)
			{
				out.write(buffer, 0, byteread);
			}
			return true;
		} catch (Exception e)
		{
			return false;
		} finally
		{
			try
			{
				if (out != null) out.close();
				if (in != null) in.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	/**
	 * 复制文件
	 * 
	 * @param source
	 * @param target
	 * @return
	 */
	public static boolean copyFile(File source, File target)
	{
		int byteread = 0;
		InputStream in = null;
		OutputStream out = null;

		try
		{
			if (!target.getParentFile().exists())
			{
				target.getParentFile().mkdirs();
			}
			in = new FileInputStream(source);
			out = new FileOutputStream(target);
			byte[] buffer = new byte[1024];
			while ((byteread = in.read(buffer)) != -1)
			{
				out.write(buffer, 0, byteread);
			}
			return true;
		} catch (Exception e)
		{
			return false;
		} finally
		{
			try
			{
				if (out != null) out.close();
				if (in != null) in.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	/**
	 * 创建文件
	 * 
	 * @param file 文件
	 * @param content 文件内容
	 * @return
	 * @throws Exception
	 */
	public static boolean mkfile(File file, String content)
	{
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		try
		{
			if (!file.getParentFile().exists())
			{
				file.getParentFile().mkdirs();
			}
			fos = new FileOutputStream(file);
			osw = new OutputStreamWriter(fos, "UTF-8");
			osw.write(content);
			osw.flush();
			return true;
		} catch (Exception ex)
		{
			ex.printStackTrace();
			return false;
		} finally
		{
			try
			{
				if (fos != null)
				{
					fos.close();
				}
				if (osw != null)
				{
					osw.close();
				}
			} catch (Exception ex)
			{
				ex.printStackTrace();
			}
		}
	}
}
